package com.witdata.controller.message;

import com.witdata.common.domain.Result;
import com.witdata.common.domain.Status;
import com.witdata.common.domain.dto.SessionDto;
import com.witdata.common.exception.GlobalException;
import com.witdata.common.domain.res.SessionResponse;
import com.witdata.framework.service.message.SessionService;
import lombok.RequiredArgsConstructor;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/message/session")
public class SessionController {
    private final SessionService sessionService;

    @PostMapping("/create")
    public Result<SessionDto> createSession(@RequestBody SessionDto sessionDto) {
        if (ObjectUtils.isEmpty(sessionDto.getReceiverId()) && ObjectUtils.isEmpty(sessionDto.getSenderId()))
            throw new GlobalException(Status.PARAMERROR);
        SessionDto session = sessionService.createSession(sessionDto);
        return Result.ok(Status.CREATESESSIONSUCCESS, session);
    }

    @GetMapping("/get")
    public Result<SessionDto> getSession(@RequestParam("senderId") Long senderId, @RequestParam("receiverId") Long receiverId) {
        if (ObjectUtils.isEmpty(senderId) && ObjectUtils.isEmpty(receiverId))
            throw new GlobalException(Status.PARAMERROR);
        SessionDto session = sessionService.getSession(senderId, receiverId);
        return Result.ok(Status.SUCCESS, session);
    }

    @GetMapping("/list")
    public Result<List<SessionResponse>> getSessionList() {
        List<SessionResponse> sessionList = sessionService.getSessionList();
        return Result.ok(Status.SUCCESS, sessionList);
    }

}
